const fs =require("fs")
const path =require("path")

const regStyle =/<style>[\s\S]*<\/style>/
const regScript = /<script>[\s\S]*<\/script>/

fs.readFile(path.join(__dirname,"./files/01.html"),"utf8",function(err,dataStr){
     if(err) return console.log("写入失败"+err);
    resolveCSS(dataStr)
    resolveScript(dataStr)
    resolveHtml(dataStr)
})

function resolveCSS(htmlStr){
    let res1 = regStyle.exec(htmlStr)
    const newCss = res1[0].replace("<style>","").replace("</style>","")
    fs.writeFile(path.join(__dirname,"./files/01.css"),newCss,function(err){
        if(err) console.log("写入文件失败"+err);
    })
}
function resolveScript(htmlStr){
    let res2 =regScript.exec(htmlStr)
    const newjs =res2[0].replace("<script>","").replace("</script>","")
    fs.writeFile(path.join(__dirname,"./files/01.js"),newjs,function(err){
        if(err) console.log("写入文件失败"+err);
    })
}
function resolveHtml(htmlStr){
    let res3 = htmlStr.replace(regStyle,"<link rel='Stylesheet' href='./01.css' />").replace(regScript,"<script src='./01.js'></script>")
    fs.writeFile(path.join(__dirname,"./files/02.html"),res3,function(err){
        if(err) console.log("写入文件失败"+err);
    })
}